package Kuang;

import java.util.Random;

/**
 * description:
 * author:zcx
 * lenovo
 * 时间：2022.02.08.20.08
 */
public class Demo5 implements Runnable {
    private static String winner;
    @Override
    public void run() {
        Random r=new Random();
        String tn=Thread.currentThread().getName();
        for (int i = 0; i <= 100; i++) {
            boolean flag=gameOver(i);
            if(tn.equals("兔子")&&i%10==0){
                try {
                    Thread.sleep(r.nextInt(2));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            if(flag){
                break;
            }
            System.out.println(tn + "--->跑了"+i+"步");
        }


    }
    private boolean gameOver(int steps) {
        if (winner != null) {
            return true;
        } else if (steps >= 100) {
            winner = Thread.currentThread().getName();
            System.out.println("winner is" + winner);
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        var t=new Demo5();
        new Thread(t,"兔子").start();
        new Thread(t,"乌龟").start();

    }

}
